package com.deram.ctin.common.utils;


import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ArrayHandleUtils {
    /**
     * 获取属性值List
     *
     * @param sourceList
     * @param attrName
     * @return
     * @author zhangcm 2016-10-31 16:28:47
     */
    public static <E> List<E> readAttrList(List<? extends Object> sourceList, String attrName) {
        return readAttrList(sourceList, attrName, true);
    }

    /**
     * 获取属性值List
     *
     * @param sourceList
     * @param attrName
     * @param isUnique
     * @return
     * @author zhangcm 2016-10-31 18:09:14
     */
    public static <E> List<E> readAttrList(Collection<? extends Object> sourceList, String attrName, Boolean isUnique) {
        Preconditions.checkNotNull(attrName);
        Preconditions.checkNotNull(isUnique);
        List<E> result = Lists.newArrayList();
        if (CollectionUtils.isEmpty(sourceList)) {
            return result;
        }
        for (Object source : sourceList) {
            E attrValue = readAttr(source, attrName);
            result.add(attrValue);
        }
        if (isUnique && CollectionUtils.isNotEmpty(result)) {
            Set<E> set = Sets.newLinkedHashSet(result);
            result = Lists.newArrayList(set);
        }
        result.remove(null);
        return result;
    }

    /**
     * 获取属性值
     *
     * @param source
     * @param attrName
     * @return
     * @author zhangcm 2016-10-31 16:28:47
     */
    public static <T> T readAttr(Object source, String attrName) {
        try {
            @SuppressWarnings("unchecked")
            T result = null != source ? (T) PropertyUtils.getProperty(source, attrName) : null;
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 去重
     * @param list
     * @return
     * @author zhangcm 2016-12-28 14:26:27
     */
    public static <T> List<T> unique(List<T> list){
        if(CollectionUtils.isEmpty(list)){
            return list;
        }
        Set<T> set = Sets.newLinkedHashSet(list);
        return Lists.newArrayList(set);
    }

    /**
     * 去重
     * @param list
     * @author zhangcm 2016-12-28 14:26:27
     */
    public static <T> void uniqueInList(List<T> list){
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        Set<T> set = Sets.newLinkedHashSet(list);
        list.clear();
        list.addAll(set);
    }

    /**
     * 把List转Map， 属性值为key，V对象为value
     * @param sourceList
     * @param attrName
     * @return
     * @author zhangcm 2016-10-31 17:07:05
     */
    public static <K, V> Map<K, V> parseMap(List<V> sourceList, String attrName) {
        Map<K, V> result = Maps.newHashMap();
        if (CollectionUtils.isEmpty(sourceList)) {
            return result;
        }
        for (V source : sourceList) {
            K attrValue = readAttr(source, attrName);
            result.put(attrValue, source);
        }
        return result;
    }

    /**
     * 对List分组
     * 根据attrName对sourceList进行分组，Map的key为attrName的值
     * @param sourceList
     * @param attrName
     * @return
     * @author zhangcm 2016-11-04 16:51:41
     */
    public static <K, V> Map<K, List<V>> parseMapGroup(List<V> sourceList, String attrName) {
        Map<K, List<V>> result = Maps.newHashMap();
        if (CollectionUtils.isEmpty(sourceList)) {
            return result;
        }
        for (V source : sourceList) {
            K attrValue = readAttr(source, attrName);
            List<V> list = result.get(attrValue);
            if (CollectionUtils.isEmpty(list)) {
                list = Lists.newArrayList();
                result.put(attrValue, list);
            }
            list.add(source);
        }
        return result;
    }
}
